<template>
  <el-dialog
    :width="'80%'"
    :visible="dialogVisible"
    :title="`${cmName}角色详情`"
    append-to-body
    @close="$emit('close')"
  >
    <el-pagination
      style="float: right;"
      layout="prev, pager, next,sizes, total"
      :total="total"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :current-page="page+1"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-table
      v-loading="loading"
      :data="data"
    >
      <el-table-column
        prop="roleType"
        label="角色类型"
      />
      <el-table-column
        prop="healthSummary"
        label="角色状态"
      >
        <template slot-scope="scope">
          <span>{{ getState(scope.row.healthSummary) }}</span>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  
</template>

<script>
import overviewApi from '@/api/monitor/overview'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    cmName: {
      type: String,
      default: ''
    },
    clusterName: {
      type: String,
      default: ''
    },
    cmFlag: {
      type: String,
      default: ''
    },
    clusterFlag: {
      type: String,
      default: ''
    }
  },
  emits: ['close'],
  data() {
    return {
      page: 0,
      size: 10,
      data: [],
      total: 0,
      loading: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.size = 10
        this.getData()
      }
    }
  },
  methods: {
    getData(resetPage = true) {
      this.loading = true
      this.page = resetPage ? 0 : this.page
      const params = {
        page: this.page,
        size: this.size,
        cmFlag: this.cmFlag,
        clusterFlag: this.clusterFlag,
        cmName: this.cmName,
        clusterName: this.clusterName
      }
      overviewApi.queryRoleStatePage(params).then(res => {
        this.data = res.content
        this.total = res.totalElements
      }
      ).finally(() => {
        this.loading = false
      })
    },
    handleCurrentChange(page) {
      this.page = page - 1
      this.getData(false)
    },
    handleSizeChange(size) {
      this.size = size
      this.getData(true)
    },
    getState(state) {
      switch (state) {
        case 'BAD_HEALTH':
          return '出现问题'
        case 'CONCERNING_HEALTH':
          return '存在隐患'
        case 'GOOD_HEALTH':
          return '良好'
        case 'NONE':
          return '不适用'
        case 'STOPED':
          return '已停止'
        case 'DOWN':
          return '失败停止'
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
